package com.sihai.ehr.service.impl;

import java.util.Calendar;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

import com.sihai.core.DataAccessor;
import com.sihai.core.DynamicQuery;
import com.sihai.core.Limit;
import com.sihai.core.LimitedList;
import com.sihai.core.QueryCreator;
import com.sihai.core.Sorter;
import com.sihai.ehr.model.biz.ExamPeriod;
import com.sihai.ehr.model.biz.ExamQualCoerceSet;
import com.sihai.ehr.model.sys.ExaminePeriodEnum;
import com.sihai.ehr.service.ExamPeriodService;

/**
 * 考核周期服务
 * 
 * @author Henry
 * 
 */
@Service("examPeriodService")
public class ExamPeriodServiceImpl implements ExamPeriodService {

	@Resource
	DataAccessor dataAccessor;

	@Override
	public ExamPeriod getExamPeriod(Long id) {
		if (id == null || id < 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			return dataAccessor.get(ExamPeriod.class, id);
		} catch (Exception e) {
			throw new RuntimeException("【异常原因：获取考核周期对象对象数据库发生异常】【异常说明：+"
					+ e.getMessage()
					+ "】【发生地点：ExamPeriodServiceImpl.getExamPeriod;参数id=" + id
					+ " 】");
		}
	}

	@Override
	public Long getExamPeriodByTypeAndYMonthly(
			final ExaminePeriodEnum periodType, final int examYear,
			final int examMonthly) {

		if (periodType == null || examMonthly <= 0) {
			throw new RuntimeException("【传入参数非法】");
		}
		try {
			Long periodId = dataAccessor.findUnique(Long.class,
					new QueryCreator() {
						@Override
						public Query createQuery(Session session) {
							Query query = session
									.createQuery("select id from ExamPeriod e where e.periodType = :periodType and e.examYear = :examYear and e.examMonthly = :examMonthly");
							query.setParameter("periodType", periodType);
							query.setParameter("examYear", examYear);
							query.setParameter("examMonthly", examMonthly);
							return query;
						}
					});
			return periodId == null ? 0L : periodId;
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核周期对象对象数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamPeriodServiceImpl.getExamPeriodByName;参数periodType="
							+ periodType + " 】");
		}
	}

	@Override
	public LimitedList<ExamPeriod> searchExamPeriods(
			final ExaminePeriodEnum examinePeriodEnum, Limit limit,
			Sorter sorter) {
		if (examinePeriodEnum == null) {
			throw new RuntimeException("【传入参数非法】【examinePeriodEnum为空】");
		}
		try {
			return dataAccessor.findByQuery(ExamPeriod.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							Calendar mCalendar = Calendar.getInstance();
							mCalendar.add(Calendar.MONTH, -1);
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from ExamPeriod e where e.examYear <> "
											+ mCalendar.get(Calendar.YEAR)
											+ " and e.examMonthly <> "
											+ (mCalendar.get(Calendar.MONTH) - 1));
							dynamicQuery.appendIfNotNull(
									"and e.periodType =:periodType",
									"periodType", examinePeriodEnum);

							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核周期列表时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamPeriodServiceImpl.searchExamPeriods;参数examinePeriodEnum="
							+ examinePeriodEnum + "】");
		}
	}

	@Override
	public LimitedList<ExamPeriod> searchExamPeriods(
			final ExamPeriod examPeriod, Limit limit, Sorter sorter) {
		if (examPeriod == null) {
			throw new RuntimeException("【传入参数非法】【examPeriod为空】");
		}
		try {
			return dataAccessor.findByQuery(ExamPeriod.class,
					new QueryCreator() {

						@Override
						public Query createQuery(Session session) {
							DynamicQuery dynamicQuery = new DynamicQuery(
									"from ExamPeriod e where 1 <> 2");
							dynamicQuery.appendIfNotNull(
									"and e.periodType =:periodType",
									"periodType", examPeriod.getPeriodType());
							return dynamicQuery.build(session);
						}
					}, limit, sorter);
		} catch (Exception e) {
			throw new RuntimeException(
					"【异常原因：获取考核周期列表时数据库发生异常】【异常说明：+"
							+ e.getMessage()
							+ "】【发生地点：ExamPeriodServiceImpl.searchExamPeriods;参数examPeriod="
							+ examPeriod + "】");
		}
	}

	@Override
	public Long saveExamPeriod(ExamPeriod examPeriod) {
		if (examPeriod == null) {
			throw new RuntimeException("【传入参数非法】【examPeriod为空】");
		}
		Long peroidId = getExamPeriodByTypeAndYMonthly(
				examPeriod.getPeriodType(), examPeriod.getExamYear(),
				examPeriod.getExamMonthly());
		if (peroidId > 0) {
			return peroidId;
		} else {
			try {
				dataAccessor.saveOrUpdate(examPeriod);
				return examPeriod.getId();
			} catch (Exception e) {
				throw new RuntimeException("【异常原因：保存考核周期对象到数据库发生异常】【异常说明："
						+ e.getMessage()
						+ "】【发生地点：ExamPeriodServiceImpl.saveExamPeriod】");
			}
		}
	}

	@Override
	public void removeExamPeriods(String examPeriodIds) {
		// TODO Auto-generated method stub

	}

}
